package com.guchenbo.example.ds.array;

/**
 * 这个是自定义的容器类，无序数组
 * Created by CreGu on 2016/5/25.
 */
public class HighArray {
    private long[] arr;
    private int index;

    public HighArray(int size) {
        arr = new long[size];
        index = 0;
    }

    public void insert(long element) {
        arr[index++] = element;
    }

    /**
     * 无序数组，线性查找
     *
     * @param element
     * @return
     */
    private int findInternal(long element) {
        for (int i = 0; i < arr.length; i++) {
            if (element == arr[i]) {
                return i;
            }
        }
        return -1;
    }

    public boolean find(long element) {
        return findInternal(element) != -1;
    }

    public boolean delete(long element) {
        int i = findInternal(element);

        if (i != -1) {
            for (int j = i; j < index - 1; j++) {
//           后面的覆盖前面的
                arr[j] = arr[j + 1];
            }
            index--;
            return true;
        } else {
            return false;
        }
    }

    public void display() {
        for (int i = 0; i < index; i++) {
            System.out.println(arr[i]);
        }
    }
}
